﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// qt headers
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>

// custom headres
#include "ParametersEditingCard.h"
#include "EditingParametersTableView.h"
#include "EditingParametersTableModel.h"
#include "IParameter.gcs.h"
#include "IParameterDefinition.gcs.h"
#include "MessageDialog.h"
#include "GbmpNew.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 
namespace gmepgcs
{

    ParametersEditingCard::ParametersEditingCard(QString &title, QString &subTitle, QWidget *parent) :
        CustomDrawer(title, subTitle, parent),
        m_pBtnDelete(nullptr),
        m_pBtnAdd(nullptr),
        m_pTable(nullptr),
        m_title(title),
        m_subTitle(subTitle)
    {
        InitUI();
    }

    ParametersEditingCard::~ParametersEditingCard()
    {
    }

    void ParametersEditingCard::SetParameters(std::vector<std::shared_ptr<IParameter>> paramters)
    {
        if (!m_pTable)
        {
            return;
        }

        m_pTable->SetParameters(paramters);
        emit ParametersChanged();
    }

    void ParametersEditingCard::UpdateParameterValue(const std::wstring & parameterName, const std::wstring & parameterOldValue, const std::wstring & parameterNewValue)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pTable, L"m_pTable为空", L"GMEP", L"2024-01-30");
        auto model = dynamic_cast<EdittingParametersTableModel *>(m_pTable->model());
        if (model)
        {
            model->UpdateParameterValue(parameterName, parameterOldValue, parameterNewValue);
        }
    }

    void ParametersEditingCard::InitUI()
    {
        QWidget *pCentralWidget = NEW_AS_QT(QWidget);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pCentralWidget, L"pCentralWidget为空", L"GMEP", L"2024-01-30");
        pCentralWidget->setObjectName("CentralWidget");
        SetCentralWidget(pCentralWidget);

        QVBoxLayout *pCentralLayout = NEW_AS_QT(QVBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pCentralLayout, L"pCentralLayout为空", L"GMEP", L"2024-01-30");
        pCentralLayout->setContentsMargins(0, 0, 0, 0);
        pCentralLayout->setSpacing(0);
        pCentralWidget->setLayout(pCentralLayout);

        // params table
        m_pTable = NEW_AS_QT(EdittingParametersTableView);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pTable, L"m_pTable为空", L"GMEP", L"2024-01-30");
        QString tableName = QString::fromStdWString(L"%1_TableView").arg(m_title);
        m_pTable->setObjectName(tableName);
        pCentralLayout->addWidget(m_pTable);
        pCentralLayout->addStretch();

        m_pTable->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Policy::Fixed);
        m_pTable->SetSizeToContent(true);
        auto model = dynamic_cast<EdittingParametersTableModel*>(m_pTable->model());
        // 暂时注释， 2023目前不支持自定义参数
        //connect(model, &EdittingParametersTableModel::ModifyNameOfParameterRequired, this, &ParametersEditingCard::ModifyNameOfParameterRequired);
        connect(model, &EdittingParametersTableModel::ModifyValueOfParameterRequired, this, &ParametersEditingCard::ModifyValueOfParameterRequired);
        //connect(model, &EdittingParametersTableModel::ModifyUnitOfParameterRequired, this, &ParametersEditingCard::ModifyUnitOfParameterRequired);
        //connect(model, &EdittingParametersTableModel::ModifyIsInstanceOfParameterRequired, this, &ParametersEditingCard::ModifyIsInstanceOfParameterRequired);
        connect(m_pTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ParametersEditingCard::SelectionChanged);

        // accessories
        QString btnStyle(
            "QPushButton{"
            "border:none;"
            "border-radius:2px;"
            "background-color:rgba(217, 221, 228, 255);"
            "}"
        );
        QCursor btnCursor = QCursor(Qt::CursorShape::PointingHandCursor);

        // delete button
        m_pBtnDelete = NEW_AS_QT(QPushButton,QIcon(":/images/icon_delete_bin_solid_373F48_16_16.png"), QString());
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBtnDelete, L"m_pBtnDelete为空", L"GMEP", L"2024-01-30");
        QString deleteButtonName = QString::fromStdWString(L"%1_DeteteButton").arg(m_title);
        m_pBtnDelete->setObjectName(deleteButtonName);
        m_pBtnDelete->setFixedSize(QSize(16, 16));
        m_pBtnDelete->setIconSize(QSize(12, 12));
        m_pBtnDelete->setCursor(btnCursor);
        m_pBtnDelete->setStyleSheet(btnStyle);
        m_pBtnDelete->setEnabled(false);
        AddAccessory(m_pBtnDelete);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pTable->selectionModel(), L"m_pTable->selectionModel()为空", L"GMEP", L"2024-01-30");
        connect(m_pBtnDelete, &QPushButton::clicked, [=]() {
            auto currentIndex = m_pTable->selectionModel()->currentIndex();
            IParameter *pParameter = static_cast<IParameter *>(currentIndex.internalPointer());
            if (pParameter && (pParameter->Definition))
            {
                emit RemoveParameterRequired(pParameter->Definition->GetName());
            }
        });

        // add button
        m_pBtnAdd = NEW_AS_QT( QPushButton,QIcon(":/images/icon_plus_sharp_373F48_16_16.png"), QString());
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBtnAdd, L"m_pBtnAdd为空", L"GMEP", L"2024-01-30");
        QString addButtonName = QString::fromStdWString(L"%1_AddButton").arg(m_title);
        m_pBtnAdd->setObjectName(addButtonName);
        m_pBtnAdd->setFixedSize(QSize(16, 16));
        m_pBtnAdd->setIconSize(QSize(12, 12));
        m_pBtnAdd->setCursor(btnCursor);
        m_pBtnAdd->setStyleSheet(btnStyle);
        AddAccessory(m_pBtnAdd);     
        connect(m_pBtnAdd, &QPushButton::clicked, [=]() {
            emit AddingParamtersRequired();
        });

        connect(m_pTable->selectionModel(), &QItemSelectionModel::currentRowChanged,
            [=](const QModelIndex &current, const QModelIndex &previous) {
            bool canDelete = false;
            IParameter *pParameter = static_cast<IParameter *>(current.internalPointer());
            if (pParameter)
            {
                canDelete = pParameter->IsDeleteable();
            }
            m_pBtnDelete->setEnabled(canDelete);
        });
    }
}

